<?php 
namespace app\admin\validate;
use app\admin\code\AdminCode;
use app\admin\code\BaseCode;
use app\admin\model\AdminModel;
use app\admin\model\BaseModel;
use think\Db;

class LoginValidate extends BaseValidate
{
    protected $_rule = [
        'user_name'  =>  'require|check_exist',
        'pass_word' =>  'require|check_pass_word',
        'verify_code' => 'require|captcha'
    ];
    protected $_message = [
        'user_name.require' => BaseCode::PARAM_CANT_EMPTY,
        'pass_word.require' => BaseCode::PARAM_CANT_EMPTY,
        'verify_code.require' => BaseCode::PARAM_CANT_EMPTY,
        'verify_code.captcha' => BaseCode::PARAM_WRONG,
        'user_name.check_exist' => BaseCode::PARAM_NOT_EXIST,
        'pass_word.check_pass_word' => BaseCode::PARAM_WRONG,
    ];
    protected $_field = [
        'user_name'=>'用户名',
        'pass_word'=>'登录密码',
        'verify_code'=>'验证码',
    ];
    protected $_scene = [
        'login'  =>  ['user_name','verify_code','pass_word'],
    ];
    protected $_extend_rule = ['check_exist','check_pass_word'];

    public $user_data = [];
    public function check_exist($data)
    {
        $model = new AdminModel();
        $result = $model->where(['user_name'=>$data])->find();
        if($result === false){
            return false;
        }else{
            $this->user_data = $result;
            return true;
        }
    }
    public function check_pass_word($data){
        $data = md5($data.$this->user_data['salt']);
        if($this->user_data['pass_word'] != $data){
            return false;
        }else{
            return true;
        }
    }
}


